<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content=""/>
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/css/app.css">
    <style>
        .card {
            border-radius: 4px;
            border: 1px solid #ebeef5;
            background-color: #fff;
            overflow: hidden;
            color: #303133;
            transition: .3s;
            margin: 10px 0;
            box-shadow: 0 2px 12px 0 rgba(0, 0, 0, .1);
            padding: 20px;
        }

        #hero {
            background-image: linear-gradient(141deg, red, #00d1b2 71%, #00e7eb 100%);
        }
    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/urlutil.js"></script>
    <script src="js/loginutil.js"></script>
</head>
<body>
<nav id="navbar" class="navbar has-shadow">
    <div class="container">
        <div class="navbar-brand">
            <div class="navbar-burger burger" data-target="navMenu">
                <span></span>
                <span></span>
                <span></span>
            </div>
        </div>
        <div id="navMenu" class="navbar-menu">
            <div class="navbar-start">
                <a class="navbar-item" href="index.html">主页</a>
                <a class="navbar-item" href="about.html">关于</a>
            </div>
            <div class="navbar-end">
                <div id="login_div" style="display: none;" class="navbar-item">
                    <a id="category_href" style="display: none" class="button is-small is-primary" href="category.html">分类管理</a>&nbsp;&nbsp;&nbsp;
                    <a class="button is-small is-primary" href="add-article.html">写文章</a>&nbsp;&nbsp;&nbsp;
                    <a id="login_username" href="user-articlelist.html"></a>&nbsp;&nbsp;&nbsp;
                    <a href="javascript:logout();">退出</a>
                </div>
                <div id="unlogin_div" class="navbar-item">
                    <a class="button is-small is-primary" href="register.html">注册</a>&nbsp;&nbsp;&nbsp
                    <a class="button is-small" href="login.html">登录</a>
                </div>
            </div>
        </div>
    </div>
</nav>
<script>
    jQuery(function () {
        var login_username = window.localStorage.getItem("username");
        if (login_username == null) {
            // 未登录
            jQuery("#unlogin_div").show();
        } else { // 已经登录
            jQuery("#unlogin_div").hide();
            jQuery("#login_username").html(login_username);
            jQuery("#login_username").attr("href", "user-articlelist.html?uid=" +
                window.localStorage.getItem("uid"));
            jQuery("#login_div").show();
            // 管理员权限判断
            if (window.localStorage.getItem("manager") == 1) {
                jQuery("#category_href").show();
            }
        }
    });
</script>

<section id="hero" class="hero is-primary is-bold">
    <div class="hero-body">
        <div class="container">
            <h1 class="title">
                技术博客
            </h1>
            <h2 class="subtitle">
            </h2>
        </div>
    </div>
</section>
<div class="container margin-top">
    <div class="columns">
        <div class="column is-8 is-offset-2">
            <div id="artlist">

            </div>
            <div>
                <nav class="pagination is-small margin-top" role="navigation" aria-label="pagination">
                    <a href="javascript:doFirst();" class="pagination-previous">首页</a>
                    <a href="javascript:doBefore();" class="pagination-previous">上一页</a>
                    <a href="javascript:doNext();" class="pagination-next">下一页</a>
                    <a href="javascript:doLast();" class="pagination-next">末页</a>
                    <ul class="pagination-list" id="totalmsg">

                    </ul>
                </nav>
                <p style="display: none;" class="has-text-centered margin-top is-size-7 has-text-grey">
                    没有更多了... </p>
            </div>
        </div>

    </div>
</div>
<script>
    var pindex = 1; // 当前页码
    var pages = 1; // 总共有多少页
    // 尝试获取参数中的页码
    var pindex_param = getUrlParam("pindex");
    if (pindex_param != null && pindex_param > 1) {
        pindex = pindex_param;
    }

    // ajax 请求方法
    function doAjax(pindex_param) {
        jQuery.ajax({
            url: req_base_url + "art/getlistbypage",
            method: "GET",
            data: {
                "pIndex": pindex_param
            },
            success: function (res) {
                // 将执行的结果展示给用户
                if (res != null && res.code == 200 &&
                    res.data != null) {
                    // 成功
                    var listHtml = "";
                    for (var i = 0; i < res.data.records.length; i++) {
                        var art = res.data.records[i];
                        listHtml += '<div class="card">';
                        listHtml += '<div class="card-content">';
                        listHtml += '<div class="content">';
                        listHtml += '<a href="detail.html?aid=' + art.aid
                            + '">' + art.title + '</a>';
                        listHtml += '</div>';
                        listHtml += '</div>';
                        listHtml += '</div>';
                    }
                    jQuery("#artlist").html(listHtml);
                    // 初始化分页数据
                    jQuery("#totalmsg").html("总共：" + res.data.total + " 条，共："
                        + res.data.pages + " 页");
                    pages = res.data.pages;
                } else {
                    // 失败
                    alert("抱歉：页面数据加载失败！" + res.msg);
                }
            }
        });
    }

    // 去首页
    function doFirst() {
        var curr_pindex = parseInt(pindex);
        if (curr_pindex <= 1) { // 已经在首页
            alert("已经在首页了！");
            return false;
        }
        location.href = "index.html";
    }

    // 去上一页
    function doBefore() {
        var curr_pindex = parseInt(pindex);
        if (curr_pindex <= 1) { // 已经在首页
            alert("已经在首页了！");
            return false;
        }
        --curr_pindex;
        location.href = "index.html?pindex=" + curr_pindex;
    }

    // 去下一页
    function doNext() {
        var curr_pindex = parseInt(pindex);
        if (curr_pindex >= pages) { // 已经在末页
            alert("已经在末页了！");
            return false;
        }
        ++curr_pindex;
        location.href = "index.html?pindex=" + pages;
    }

    // 去末页
    function doLast() {
        var curr_pindex = parseInt(pindex);
        if (curr_pindex >= pages) { // 已经在末页
            alert("已经在末页了！");
            return false;
        }
        location.href = "index.html?pindex=" + pages;
    }

    // 初始化页面数据
    function initPage() {
        doAjax(pindex);
    }

    initPage();

</script>
</body>
</html>

